Parameters m.CodServico, m.CodSubServ

Private aliold, ordold1, ordold2, regold1, regold2, ltalk
Private CodSub, Nivel, Ciclico

if Set('Talk') = 'ON'
   set talk off
   ltalk = .t.
  else
   ltalk = .f.
endif

m.aliold  = select()

Select Servicos
       m.ordold1 = order()
       m.regold1 = recno()

Select SubServ
       m.ordold2 = order()
       m.regold2 = recno()

m.CodSub  = m.CodSubServ
m.Nivel   = 0
m.ciclico = .f.

do VerSub with m.CodServico

Select Servicos
       set order to tag &ordold1
       
if m.regold1 > 0 and m.regold1 <= reccount()
   goto (m.regold1)
endif

Select SubServ
       set order to tag &ordold2
       
if m.regold2 > 0 and m.regold2 <= reccount()
   goto (m.regold2)
endif

Select (m.AliOld)

if ltalk
   set talk on
endif

return (m.ciclico)


Procedure   VerSub
Parameters  m.SubServico

Private m.regmov

if m.Nivel >= 25
   m.nivel = m.nivel - 1
   return
endif

m.nivel = m.nivel + 1

if m.SubServico == m.CodSub
   m.Ciclico = .t.
endif

Select SubServ
       set order to tag SubServico

Seek m.SubServico

if found() and not ciclico

   scan while SubServico = m.SubServico
   
        m.regmov = recno()

        do VerSub with SubServ.Servico
        
        if m.ciclico
           exit
        endif        
        
        Select SubServ
        goto (m.RegMov)
        
   endscan
   
endif

m.nivel = m.nivel - 1

